package utils;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import dbhelper.Comments;
import dbhelper.Items;
import entities.Comment;

public class CommentsHelper {

//private static String[] SupportedTags = {
//	"i","b","u","a href"
//};

/**
 * post a new comment
 * @param comment
 * @return whether the operation was successful or not
 */
public static boolean postComment(Comment comment){
	if(comment != null && StringUtils.isNotBlank(comment.getText()) && 
			Items.isForSaleItemStringQuery(comment.getItemID())
			){
		String escapedString = escapeHTML(comment.getText());
		if(!escapedString.isEmpty()){
		comment.setText(escapedString);
		comment.setDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));
		return Comments.postNewComment(comment);
	}
	}
	return false;
}

/**
 * return the the comment of the specified item 
 * @param itemID
 * @param pageNumber
 * @param startFrom the date to show result after (could be null)

 * @return
 */
public static List<Comment> getComments(long itemID,Timestamp startFrom, int pageNumber){
	if(pageNumber < 0) 
		pageNumber = 0;
	return (itemID != -1) ? 
			Comments.getComments(itemID,startFrom, pageNumber) : null;
}

//filters the html in the text except the supported tags
private static String escapeHTML(String text){
	return (text != null) ? StringEscapeUtils.escapeHtml4(
	StringEscapeUtils.unescapeHtml4(text)
		) : "";
}


//// filters the html in the text except the supported tags
//	private static String escapeHTML(String text){
//		StringBuilder sb = new StringBuilder(text);
//		ArrayList<CharSequence> tagsSoFar =  new ArrayList<CharSequence>();
//		int fromIndex = 0;
//		while(sb.length() >= fromIndex){
//		int startTagIndex = sb.indexOf("<", fromIndex) + 1;
//		if(startTagIndex == 0) break;
//		int endTagIndex = sb.indexOf(">", fromIndex) - 1;
//		CharSequence tagString = sb.subSequence(startTagIndex, endTagIndex);
//		if(sb.charAt(startTagIndex) != '/'){
//			//opening tag
//		if(ifSupportedTag(tagString)){
//			tagsSoFar.add(tagString);
//		}else{
//			//remove the whole tag with its ">" and "<"
//			sb.delete(startTagIndex - 1, endTagIndex + 1);
//		}
//		}else{
//			// closing tag
//			String closingTag = sb.substring(startTagIndex + 1,endTagIndex);
//			for(int i = tagsSoFar.size() - 1 ; i >= 0;i--){
//				if(closingTag.equals(tagsSoFar.get(i))){
//					tagsSoFar.remove(i);
//					break;
//				}
//			}
//		}
//		fromIndex = startTagIndex;
//		}
//		//close unclosed tags
//		for(int i = tagsSoFar.size() - 1 ; i >= 0;i--){
//			sb.append("</");
//			sb.append(tagsSoFar.get(i));
//			sb.append(">");
//		}
//		return sb.toString();
//	}
	
//	private static boolean ifSupportedTag(CharSequence tagString){
//		boolean ifSupported = false;
//		for(String supportedTag: SupportedTags){
//			 if(tagString.equals(supportedTag))
//				 ifSupported = true;
//			 break;
//		}
//		return ifSupported;
//	}
	
}
